package com.xhs.main.controller.rsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.rsb.*;
import com.xhs.main.service.rsb.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author author
 * @create 2020-10-19-21:03
 */
@Controller
public class RsbUserController {

    @Autowired
    RsbUserServiceImpl rsbUserService;
    @Autowired
    RsbAttendanceServiceImpl rsbAttendanceService;
    @Autowired
    RsbShowMoneyServiceImpl rsbShowMoneyService;
    @Autowired
    RsbMoneyServiceImpl rsbMoneyService;
    @Autowired
    RsbContractServiceImpl rsbContractService;

    //导航栏---user.index只用异步请求
    @ResponseBody
    @PostMapping("/rsb/user")
    public Integer rsbUser(RsbUser rsbUser,MultipartFile img) {
        //根据员工编号和姓名进行查询
        RsbUser user=rsbUserService.selectRsbUserByNameAndNum(rsbUser.getUserName(),rsbUser.getUserNum());
        if(user==null){
            //添加员工
            rsbUserService.insertRsbUser(rsbUser);
            //添加考勤
            RsbAttendance rsbAttendance=new RsbAttendance();
            rsbAttendance.setRsbAtNum(rsbUser.getUserName()+"kq");
            rsbAttendance.setUserNum(rsbUser.getUserNum());
            rsbAttendance.setUserName(rsbUser.getUserName());
            rsbAttendance.setRsbAtDay(22);
            rsbAttendance.setRsbAtTrueday(22);
            rsbAttendanceService.insertAttendance(rsbAttendance);
            //添加展示工资
            RsbShowMoney rsm=new RsbShowMoney();
            //添加员工编号、员工姓名、工资编号
            rsm.setUserNum(rsbUser.getUserNum());
            rsm.setUserName(rsbUser.getUserName());
            rsm.setRsbShNum(rsbUser.getUserName()+"sm");
            //添加补助（餐补、交通补贴）
            RsbMoney rsbMoney = rsbMoneyService.selectChoose(rsbUser.getUserDep(), rsbUser.getUserPost()).get(0);

            //签合约基本工资
            RsbContract rsbContract = rsbContractService.selectByUserName(rsbUser.getUserName());
            rsm.setRsbMonBasepay(Double.parseDouble(rsbContract.getRsbConBasepay()));
            rsm.setRsbMonEat(rsbMoney.getRsbMonEat());
            rsm.setRsbMonTraffic(rsbMoney.getRsbMonTraffic());
            rsm.setRsbMonPhone(rsbMoney.getRsbMonPhone());
            rsm.setRsbMonFive(rsbMoney.getRsbMonFive());
            rsm.setRsbShBeforetaxes(rsm.getRsbMonBasepay()+rsm.getRsbMonEat()+rsm.getRsbMonTraffic()+rsm.getRsbMonPhone()-rsm.getRsbMonFive());
            rsm.setRsbShAftertaxes(rsm.getRsbShBeforetaxes()*0.9);
            rsm.setRsbShGetmoney(rsm.getRsbShAftertaxes());

            rsbShowMoneyService.insertShowMoney(rsm);
            return 1;   //返回一表示添加成功
        }else{
            //修改员工
            rsbUserService.updateRsbUser(rsbUser,rsbUser.getUserNum(),rsbUser.getUserName());
            return 2;   //返回2表示修改成功
        }
        /*if(rsbUser.getUserName()!=null&&rsbUser.getUserNum()!=null){
            //根据员工编号和姓名进行查改
            RsbUser user=rsbUserService.selectRsbUserByNameAndNum(rsbUser.getUserName(),rsbUser.getUserNum());
            //user不为空则有对象，直接修改；user为空则添加对象
            if(user!=null){
                //修改
                rsbUserService.updateRsbUser(rsbUser,user.getUserNum(),user.getUserName());
                return null;
            }*/
                /*if(img!=null){
                    //头像路径
                    String path="D:\\images";
                    //避免同名覆盖
                    Date date = new Date();
                    String filename = date.toString() + img.getOriginalFilename();
                    File file = new File(path, filename);
                    *//*img.transferTo(file);*//*
                    rsbUser.setUserPhotopath(filename);
                    //添加
                    rsbUserService.insertRsbUser(rsbUser);
                    return null;
                }*/
        /*}else{
            //添加
            rsbUser.setUserNum(UUID.randomUUID().toString());
            rsbUserService.insertRsbUser(rsbUser);
            return null;
        }*/
    }

    //展示员工
    @ResponseBody
    @RequestMapping("/rsb/showUser")
    public RsbLayui showUser(RsbUser rsbUser,int page,int limit){
        int count = rsbUserService.selectUserByChoose(rsbUser).size();
        List<RsbUser> users = rsbUserService.selectUserByChooseLimit(rsbUser,(page-1)*limit,limit);

        //不用sql语句分页，直接对得到的集合进行分页
        /*List<RsbUser> users2=new ArrayList<>();
        for(int i=limit*(page-1);i<limit*page;i++){
            if(i< users.size()){
                users2.add(users.get(i));
            }
        }*/

        return new RsbLayui(0,"",count,users);
    }
    //点击员工姓名查看员工信息
    @ResponseBody
    @RequestMapping("/showEmployee")
    public RsbUser showEmployee(String employName){
        RsbUser rsbUser = rsbUserService.selectRsbUserByName(employName);
        return rsbUser;
    }

    //部门职位二级联动
    @ResponseBody
    @RequestMapping("/showPost")
    public List<String> showPost(String dep){
        List<String> posts=new ArrayList<>();
        if(dep.equals("无")){
            posts.add("董事长");
            posts.add("总经理");
        }else if(dep.equals("人事部")){
            posts.add("人事总监");
            posts.add("人事经理");
            posts.add("人事主管");
            posts.add("人事专员");
        }else if(dep.equals("研发部")){
            posts.add("研发部经理");
            posts.add("项目经理");
            posts.add("组长");
            posts.add("组员");
        }else if(dep.equals("生产部")){
            posts.add("车间主任");
            posts.add("线长");
            posts.add("组长");
            posts.add("组员");
        }else if(dep.equals("物资部")){
            posts.add("仓库部长");
            posts.add("仓库主任");
            posts.add("组长");
            posts.add("组员");
        }else if(dep.equals("财务部")){
            posts.add("首席财务官");
            posts.add("财务总监");
            posts.add("高级会计师");
            posts.add("会计师");
        }else if(dep.equals("销售部")){
            posts.add("销售经理");
            posts.add("分管经理");
            posts.add("组长");
            posts.add("组员");
        }else{
            posts.add("请先选择部门！");
        }
        return posts;
    }

    //删除员工
    @ResponseBody
    @RequestMapping("/deleteEmploy")
    public void deleteEmploy(String userNum){
        List<String> lists=new ArrayList<>();
        lists.add(userNum);
        rsbUserService.deleteUsers(lists);
    }
    //模糊搜索
    //模糊查询带分页
    @ResponseBody
    @RequestMapping("/rsb/searchEmploy")
    public RsbLayui searchEmploy(@RequestParam("condition")String condition, int page, int limit){
        //获取前端传递的json数据
        JSONObject object = JSONObject.parseObject(condition);
        String userName=(String)object.get("userName");
        String userSex=(String)object.get("userSex");
        String userDep=(String)object.get("userDep");
        String userStatus=(String)object.get("userStatus");

        RsbUser rsbUser=new RsbUser();
        rsbUser.setUserName(userName);
        rsbUser.setUserSex(userSex);
        rsbUser.setUserDep(userDep);
        rsbUser.setUserStatus(userStatus);
        //得到总条数
        int size = rsbUserService.selectUserByChoose(rsbUser).size();
        //得到数据
        List<RsbUser> rsbUsers = rsbUserService.selectUserByChooseLimit(rsbUser, (page - 1) * limit, limit);
        //返回到页面并展示数据
        return new RsbLayui(0,"",size,rsbUsers);
    }


}
